package it.java.giw.jsf;

import it.java.giw.solr.ImgSearcher;
import it.java.giw.solr.Searcher;

import java.io.Serializable;
import java.net.MalformedURLException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.apache.solr.client.solrj.SolrServerException;

@ManagedBean
@SessionScoped
public class MainBean implements Serializable {

	private static final long serialVersionUID = 1L;

	private String queryString;
	private Result result;
	private Result resultWithoutSuggestions;
	private ImgResult imgResult;
	
	
	public Result getResultWithoutSuggestions() throws MalformedURLException, SolrServerException {
		Searcher searcher = new Searcher();
		resultWithoutSuggestions = searcher.searchWithoutSuggestions(queryString);
		return resultWithoutSuggestions;
	}
	public void setResultWithoutSuggestions(Result resultWithoutSuggestions) {
		this.resultWithoutSuggestions = resultWithoutSuggestions;
	}
	public String getQueryString() {
		return queryString;
	}
	public void setQueryString(String queryString) {
		this.queryString = queryString;
	}
	public Result getResult(){
		if(result != null)
			return result;
		else
			return new Result();
	}
	
	public void setResult(Result result) {
		this.result = result;
	}
	
	public ImgResult getImgResult() throws MalformedURLException, SolrServerException {
		return imgResult;
	}
	public void setImgResult(ImgResult imgResult) {
		this.imgResult = imgResult;
	}
	
	public void search() throws MalformedURLException, SolrServerException{
		if (queryString.isEmpty())
			queryString="*:*";
		Searcher searcher = new Searcher();
		result = searcher.search(queryString);
	}
	
	public void imageSearch() throws MalformedURLException, SolrServerException{
		if (queryString.isEmpty())
			queryString="*:*";
		ImgSearcher searcher = new ImgSearcher();
		imgResult = searcher.search(queryString);
	}
	
	public void searchWithoutSuggestions() throws MalformedURLException, SolrServerException{
		Searcher searcher = new Searcher();
		result = searcher.searchWithoutSuggestions(queryString);
	}

}